import { createProxy } from './build/vite/proxy.ts'
import { createVitePlugins } from './build/vite/plugin/index.ts'
// defineConfig用于定义vite配置，loadEnv用于加载环境变量
import { defineConfig, loadEnv } from 'vite'
import pkg from './package.json'
import { resolve } from 'path'

function modeMatch(mode: string) {
  return mode === 'production' ? 'prod' : mode
}

const { version } = pkg		// 从package.json文件中获取version版本
// command:如build或serve，mode:如production或development
export default defineConfig (({command, mode}) => {
	const env = loadEnv(mode, process.cwd())
	const isBuild = command === 'build'
	return {
		base: '/',		// 应用的基路径
		resolve: {
			alias: [
				{
          find: /\/@\//,
				  replacement: `${resolve(__dirname, 'src')}`,
        }
			],
		},
		server: createProxy(),
		plugins: createVitePlugins(env, isBuild),
	    css: {		// 配置css预处理器
			preprocessorOptions: {
				less: {
					javascriptEnabled: true,	// 启用js表达式
					additionalData: `@import "${resolve(		// 全局样式文件导入
						__dirname,
						'src/assets/styles/global.less',
					)}";`,
					modifyVars: {		// 自定义变量
						'primary-color': '#FF7333',
						'table-border-color': '#D8D8D8',
						'table-header-bg': '#EFF2F6',
						'modal-header-bg': '#F5F5F5',
						'modal-body-padding': '14px',
						'modal-header-padding': '10px 15px',
						'menu-dark-color': '#fff',
						'menu-dark-bg': '#0E1B2B',
						'menu-dark-highlight-color': '#FF7333',
						'menu-dark-item-active-bg': '#25313F',
						'menu-dark-selected-item-icon-color': '#FF7333',
						'menu-dark-selected-item-text-color': '#FF7333',
						'menu-item-padding': '0 15px',
					},
				},
			},
	    },
		build: {		// 构建配置
			// 输出目录，根据模式不同:dist/prod  dist/development
			outDir: `dist/${modeMatch(mode)}`,
			// 静态资源目录，使用项目版本号命名
			assetsDir: `${version}`,
	    },
	}
})
